package com.charlie.meetingmanagement.service.impl;


import com.charlie.meetingmanagement.mapper.RentalMapper;
import com.charlie.meetingmanagement.mapper.RequestMapper;
import com.charlie.meetingmanagement.pojo.po.PageBean;
import com.charlie.meetingmanagement.pojo.po.Rental;
import com.charlie.meetingmanagement.pojo.po.Request;
import com.charlie.meetingmanagement.pojo.po.Result;
import com.charlie.meetingmanagement.pojo.vo.RuquestByTimeVo;
import com.charlie.meetingmanagement.service.RentalService;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class RentalServiceImpl implements RentalService {

    private static final Logger log = LoggerFactory.getLogger(RentalServiceImpl.class);
    @Resource
    private RentalMapper rentalMapper;
    @Resource
    private RequestMapper requestMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, Integer meetingroomId, LocalDate meetingroomRentaldate,
                         Integer meetingroomRentaltime, Integer userId, Integer approveId) {
        Long count = rentalMapper.count(meetingroomId,meetingroomRentaldate, meetingroomRentaltime,userId,approveId);
//        这里使用pageHelper进行分页，非常好用
        PageHelper.startPage(page, pageSize);
        List<Rental> rentalList = rentalMapper.page(meetingroomId,meetingroomRentaldate, meetingroomRentaltime,userId,approveId);
        return new PageBean(count, rentalList);
    }

    @Override
    public Result insertRental(Rental rental) {
        try {
            rentalMapper.insert(rental.getMeetingroomId(),rental.getMeetingroomRentaldate(),rental.getMeetingroomRentaltime(),
                    rental.getRentalDescribe(),rental.getUserId(),rental.getApproveId());
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error("INSERT_RENTAL ERROR");
        }
        return Result.success();
    }

    @Override
    public Result regretRental(Rental rental) {
        try {
            requestMapper.insert(rental.getUserId(),rental.getMeetingroomId(),rental.getMeetingroomRentaldate(),
                    rental.getMeetingroomRentaltime(),rental.getRentalDescribe(),0);
            rentalMapper.deleteByIdAndDateTime(rental);
            return Result.success();
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error("INSERT_RENTAL ERROR");
        }
    }

    @Override
    public Result deleteRental(Rental rental) {
        try {
            rentalMapper.deleteByIdAndDateTime(rental);
            return Result.success();
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error("DELETE_RENTAL ERROR");
        }
    }

    @Override
    public RuquestByTimeVo getRentalInfo(LocalDate rentaldate, Integer meetingroomId) {
        List<Request> request=requestMapper.getRentalInfo(rentaldate.toString(),meetingroomId,1);
        RuquestByTimeVo result=new RuquestByTimeVo();
        result.setRentaledList(new ArrayList<>());
        for (int i = 0; i < 6; i++) {
            boolean flag=false;
            for (int j=0;j<request.size();j++){
                if (i== request.get(j).getMeetingroomRentaltime())
                {
                    flag=true;
                    break;
                }
            }
            if(flag)
                result.getRentaledList().add(true);
            else
                result.getRentaledList().add(false);
        }
        // 对 List 进行排序
        System.out.println(rentaldate+"----"+meetingroomId);
        System.out.println(result);
        return result;
    }
}
